/*
 * util.h
 *
 *  Created on: 20 Apr 2011
 *      Author: s0965328
 */

#ifndef UTIL_H_
#define UTIL_H_

#include <stdio.h>
#include <cassert>
#include <string.h>
#include <sys/stat.h>
#include "TimeStamp.h"
#include "GlobalVariables.h"

#define DEBUG_PRINT_ENABLED 1  //enable debug print
#define PERFORMANCE_MEASURE_ENABLED 0 //enable performance measure
#define DEBUG_PAR_ENABLED 0
#define DEBUG_YAC_ENABLED 1

#if DEBUG_PRINT_ENABLED
#define DEBUG_C printf
#else
#define DEBUG_C(format, args...) ((void)0)
#endif


#if DEBUG_PRINT_ENABLED
#define LOG(arg) (std::cout<<"LOG==>["<<GlobalVariables::rank<<"/"<<GlobalVariables::size<<"]-"<<arg<<std::endl)
#else
#define LOG(arg) ((void)0)
#endif

#if DEBUG_PRINT_ENABLED
  #define LOG_SM(arg) (std::cout<<"LOG_SM==>"<<arg<<std::endl)
#else
  #define LOG_SM(arg) ((void)0)
#endif

#if DEBUG_PAR_ENABLED
#define LOG_PAR(arg) (std::cout<<"PAR_SCRIPT==>"<<arg<<std::endl)
#else
#define LOG_PAR(arg) ((void)0)
#endif

#if DEBUG_YAC_ENABLED
#define YAC_LOG(arg) (std::cout<<"YAC_LOG==>"<<arg<<std::endl)
#else
#define YAC_LOG(arg) ((void)0)
#endif


#if PERFORMANCE_MEASURE_ENABLED
#define TIMER_START(arg) (TimeStamp::GetTimeStamp(arg)->start())
#else
#define TIMER_START(arg) ((void)0)
#endif

#if PERFORMANCE_MEASURE_ENABLED
#define TIMER_STOP(arg) (TimeStamp::GetTimeStamp(arg)->stop())
#else
#define TIMER_STOP(arg) ((void)0)
#endif

#if PERFORMANCE_MEASURE_ENABLED
#define TIMER_LIST (TimeStamp::List())
#else
#define TIMER_LIST ((void)0)
#endif

#if PERFORMANCE_MEASURE_ENABLED
#define TIMER_RESET (TimeStamp::Reset())
#else
#define TIMER_RESET ((void)0)
#endif

int inline findAndReplace(string& source, const char* find, const char* replace)
{
	LOG_PAR("findAndReplace - find["<<find<<"] replace["<<replace<<"]");
    int num=0;
    size_t fLen = strlen(find);
    size_t rLen = strlen(replace);

    for (size_t pos=0; (pos=source.find(find, pos))!=string::npos; pos+=rLen)
    {
        num++;
        source.replace(pos, fLen, replace);
    }
    return num;
}

int inline createTmpDirIfNotPresent()
{
	int err = 0;
	bool fl_exists;
	struct stat sbuf;
	const char *tmpdirname = "tmp";

	fl_exists = !(stat(tmpdirname, &sbuf) == -1);

	if(!fl_exists)
	{// tmp doesn't exist
#ifdef HAVE_DIRECT_H
		err = mkdir(tmpdirname);
#else
		err = mkdir(tmpdirname, S_IRWXU);
#endif
		if(err)
			cerr << "ERROR: Failed to create temporary directory '"<< tmpdirname << "'.\n";
	}
	else
	{	// tmp is present
		// check that it's a directory with RWX permissions
		bool isusable = S_ISDIR(sbuf.st_mode) && ((sbuf.st_mode & S_IRWXU)== S_IRWXU);
		if (!isusable)
		{
			err = 1;
			cerr << "ERROR: Cannot use '" << tmpdirname<< "' as temporary directory.\n";
		}
	}
	return err;
}

/* ---------------------------------------------------------------------------
bool is_int(char *tok)
---------------------------------------------------------------------------- */
/** Checks if a cstring represents a natural number (i.e [0-9]*) or not */
bool inline is_int(const char *tok){
  const char *cp = tok;
  if (!cp) return false;
  while (*cp)
    if (!isdigit(*cp++)) return false;
  return true;
}


#endif /* UTIL_H_ */
